﻿using RsaRegister.Model;
using RsaRegister;
using SoftRegManager.BLL;
using SoftRegManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SoftRegManager.Forms
{
    public partial class FrmSoftInfoEdit : Form
    {
        public SoftInfo SoftInfo { get; private set; }
        private GreatBLL mBLL;
        public FrmSoftInfoEdit(GreatBLL bLL, SoftInfo softInfo=null)
        {
            InitializeComponent();
            SoftInfo = softInfo;
            mBLL = bLL;
        }

        private void FrmSoftInfoEdit_Load(object sender, EventArgs e)
        {
            if (SoftInfo == null)
            {
                SoftInfo = new SoftInfo
                {
                    SoftId = Guid.Empty,
                    SoftName = "软件名称",
                    LastVersion = "1.0.0",
                    PriKey = "",
                    DownloadAddress = "ftp://site1.com/download/app.zip///ftp://site2.com/download/app.zip"
                };
            }
            LoadSoftInfo(SoftInfo);
        }
        private void LoadSoftInfo(SoftInfo softInfo)
        {
            txtId.Text = softInfo.SoftId.ToString();
            txtSoftName.Text = softInfo.SoftName;
            txtLastVersion.Text = softInfo.LastVersion;
            txtPrikey.Text = softInfo.PriKey;
            txtPubkey.Text = softInfo.PubKey;
            txtDownLoadAddress.Text = softInfo.DownloadAddress;
        }

        private void btnGenPrikey_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty (SoftInfo.PriKey))
            {
               if( MessageBox.Show("检测到软件已经存在私钥，更新私钥会导致旧注册码失效。\r\n确定要生成新私钥码？",
                    "警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)== DialogResult.No)
                {
                    return;
                }
            }
            try
            {
                RsaServer rsaServer = new RsaServer();
                rsaServer.GenerateKeys();
                //SoftInfo.PriKey = rsaServer.priKey;
                txtPrikey.Text = SoftInfo.PriKey = rsaServer.priKey;
                txtPubkey.Text = SoftInfo.PubKey;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            
            SoftInfo.SoftName = txtSoftName.Text;
            SoftInfo.LastVersion = txtLastVersion.Text;
            SoftInfo.DownloadAddress = txtDownLoadAddress.Text;
            SoftInfo.PriKey = txtPrikey.Text;
            try
            {
                SoftInfo = mBLL.SaveSoftwareInfo(SoftInfo);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"保存软件信息时出错",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            
        }

        private void btnReadFromFIle_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog
            {
                Title = "指定程序文件，获取文件软件信息",
                Filter = "可执行文件(*.EXE)|*.EXE|动态链接库文件(*.DLL)|*.DLL|所有文件(*.*)|*.*",
                RestoreDirectory = true
            };
            if(fd.ShowDialog()== DialogResult.OK)
            {
                var prdInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(fd.FileName);
                if(prdInfo != null)
                {
                    txtSoftName.Text = prdInfo.ProductName;
                    txtLastVersion.Text = prdInfo.ProductVersion;
                }
                else
                {
                    MessageBox.Show("未获取到软件信息","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }

            }
            
            
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog
            {
                Title = "指定存放密钥的文件。用于导入已生成过的密钥。",
                Filter = "密钥文件(*.key;*.dat)|*.key;*.dat|所有文件(*.*)|*.*",
                RestoreDirectory = true
            };
            if (fd.ShowDialog() == DialogResult.OK)
            {
                string key = File.ReadAllText(fd.FileName);
                //测试key是否合格
                var soft = new SoftInfo()
                {
                    SoftName = txtSoftName.Text,
                    PriKey = key
                };
                if(string.IsNullOrEmpty( soft.PubKey))
                {
                    MessageBox.Show("指定的文件没有私钥信息","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                txtPrikey.Text = key;

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
